-- Coords
-- create by zouyb
-- 坐标相关工具函数

-- 百度坐标转经纬坐标
function baidu2LngLat(baiduX, baiduY, f)
    local mapLayer = lbs.LBSMapLayer:getInstance();
    local node = tolua.cast(mapLayer,"cc.Node")

    local function getResult()
        local str = mapLayer:baidu2LngLat(baiduX,baiduY);
        if (str == "") then
            performWithDelay(node, getResult, 0.5);
            return;
        end

        print("str=".. str);
        local arr = string.explode(str, ",");
        local lng = tonumber(arr[1]);
        local lat = tonumber(arr[2]);

        if  ( f ~= nil ) then
            f(lng, lat);
        end
    end
    performWithDelay(node, getResult, 0.1);
end

-- 经纬坐标转百度坐标
function lngLat2Baidu(lng, lat, f)
    local mapLayer = lbs.LBSMapLayer:getInstance();
    local node = tolua.cast(mapLayer,"cc.Node")

    local function getResult()
        local str = mapLayer:lnglat2Baidu(lng,lat);
        if (str == "") then
            performWithDelay(node, getResult, 0.5);
            return;
        end

        print("str=".. str);
        local arr = string.explode(str, ",");
        local baiduX = tonumber(arr[1]);
        local baiduY = tonumber(arr[2]);

        if  ( f ~= nil ) then
            f(baiduX, baiduY);
        end
    end
    performWithDelay(node, getResult, 0.1);
end

-- converts degrees to radians
function degreesToRadians(angle)
    return angle * 0.01745329252;  -- PI / 180
end

-- converts radians to degrees
function radiansToDegrees(angle)
    return angle * 57.29577951;  -- PI * 180
end

-- 计算两点之间的角度
function calcAngle(fromPos, toPos)
    -- 角度修正
    local deltalX = toPos.x - fromPos.x;
    local deltalY = toPos.y - fromPos.y;

    -- 计算两点间的距离
    local distance = math.sqrt(math.pow(deltalX, 2) + math.pow(deltalY, 2));

    -- 计算旋转角度
    local ang;
    if toPos.y > fromPos.y then
        -- 第一、二象限
        ang = math.acos(deltalX / distance)
    else
        -- 第三、四象限
        ang = 2*math.pi - (math.acos(deltalX / distance));
    end

    -- 将弧度转成度
    ang = radiansToDegrees(ang);
    return ang;
end



